const fs = require('fs');
const path = require('path');
const Excel = require('exceljs');

let text = 'hello';
let num = 10;
let src = './old/';
let dst = './new/';
async function treat(file) {
    let workbook = new Excel.Workbook();
    await workbook.xlsx.readFile(src + file);
    let worksheet = workbook.getWorksheet(1);

    let total = worksheet.rowCount;
    let max_insert = Math.ceil(total / num);
    //console.log(total);
    for (let i = 1; i < max_insert; i++) {
        worksheet.insertRow(num * i + i, [text]);
    }
    worksheet.addRow([text]);
    await workbook.xlsx.writeFile(dst + file);
}
async function start(){
    let tmp=fs.readdirSync(dst);
    for(let file of tmp){
        if(file=='.' || file=='..') continue;
        fs.unlinkSync(dst+file);
    }
    console.log('只支持xlsx格式的文档！')
    let files = fs.readdirSync(src);
    for (let [index, file] of files.entries()) {
        let ext = path.extname(file);
        if (ext != '.xlsx') continue;
        console.log('正在处理第' + (index + 1) + '个文档。');
        await treat(file);
    }
    console.log('ok');
}
start();


